package com.atguigu.guli.service.vod.service.impl;

import com.aliyun.vod.upload.impl.UploadVideoImpl;
import com.aliyun.vod.upload.req.UploadStreamRequest;
import com.aliyun.vod.upload.resp.UploadStreamResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthRequest;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthResponse;
import com.atguigu.guli.service.base.exception.GuliException;
import com.atguigu.guli.service.base.result.ResultCodeEnum;
import com.atguigu.guli.service.vod.service.MediaService;
import com.atguigu.guli.service.vod.util.VodProperties;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

@Service
@Slf4j
public class MediaServiceImpl implements MediaService {

    @Autowired
    VodProperties vodProperties;

    //将视频上传到VOD 返回视频的id
    @Override
    public String upload(MultipartFile file) {
        try {
            UploadStreamRequest request = new UploadStreamRequest(vodProperties.getAccessKeyId(),
                    vodProperties.getAccessKeySecret(),
                    file.getOriginalFilename(),
                    file.getOriginalFilename(),
                    file.getInputStream());
            //工作流id
            request.setWorkflowId(vodProperties.getWorkflowId());
            //new HttpClient 创建客户端对象
            UploadVideoImpl uploader = new UploadVideoImpl();
            //client.excute(httpPost)
            UploadStreamResponse response = uploader.uploadStream(request);
            if (response.isSuccess()){
                System.out.println("VideoId = "+response.getVideoId());
                return response.getVideoId();
            }else{
                /* 如果设置回调URL无效，不影响视频上传，可以返回VideoId同时会返回错误码。其他情况上传失败时，VideoId为空，此时需要根据返回错误码分析具体错误原因 */
                log.error("VideoId=" + response.getVideoId() + "\n");
                log.error("ErrorCode=" + response.getCode() + "\n");
                log.error("ErrorMessage=" + response.getMessage() + "\n");
                throw new GuliException(ResultCodeEnum.VIDEO_UPLOAD_ALIYUN_ERROR ,
                        new RuntimeException(response.getMessage()));
            }

        } catch (Exception e) {
            throw new GuliException(ResultCodeEnum.VIDEO_UPLOAD_ALIYUN_ERROR  ,e);
        }
    }

    @Override
    public String getPlayUrl(String videoId) {
        return null;
    }

    @Override
    public String getPlayAuth(String videoId) {

        DefaultProfile profile = DefaultProfile.getProfile(vodProperties.getRegionId(),
                vodProperties.getAccessKeyId(),
                vodProperties.getAccessKeySecret());
        DefaultAcsClient client = new DefaultAcsClient(profile);
        //创建请求对象
        GetVideoPlayAuthRequest request = new GetVideoPlayAuthRequest();
        request.setVideoId(videoId);
        request.setAuthInfoTimeout(vodProperties.getTimeout());
        try {
            //获取响应
            GetVideoPlayAuthResponse response = client.getAcsResponse(request);
            System.out.println("PlayAuth = "+ response.getPlayAuth() + "\n");
            return response.getPlayAuth();
        } catch (Exception e) {
            throw new GuliException(ResultCodeEnum.FETCH_PLAYAUTH_ERROR , e);
        }
    }
}
